<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拦截器</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
    // 设置请求拦截器
    axios.interceptors.request.use(function (config) {
        console.log("request interceptor success-1");
        // 修改config中的参数
        config.params = {a: 100};
        return config;
    }, function (error) {
        console.log("request interceptor failure-1");
        return Promise.reject(error);
    });
    axios.interceptors.request.use(function (config) {
        console.log("request interceptor success-2");
        // 修改config中的参数
        config.timeout = 2000;
        return config;
    }, function (error) {
        console.log("request interceptor failure-2");
        return Promise.reject(error);
    });

    // 设置响应拦截器
    axios.interceptors.response.use(function (response) {
        console.log("response interceptor success-1");
        // console.log(response);
        // console.log(response.data);
        // return response;
        return response.data;
    }, function (error) {
        console.log("response interceptor failure-1");
        return Promise.reject(error);
    });

    axios.interceptors.response.use(function (response) {
        console.log("response interceptor success-2");
        return response;
    }, function (error) {
        console.log("response interceptor failure-2");
        return Promise.reject(error);
    });

    // 发送请求
    axios({
        method: 'GET',
        url: 'http://localhost:3000/posts'
    }).then(resp => {
        console.log('自定义回调，处理成功的结果');
        console.log(resp);
    }).catch((reason)=>{
        console.log('自定义失败回调');
    });
</script>
</body>
</html>